package com.hgyc.mom.base.controller;

import java.util.*;

import com.hgyc.mom.base.entity.BomItem;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.base.entity.Bom;
import com.hgyc.mom.base.vo.BomParam;
import com.hgyc.mom.base.service.BomService;
import com.hgyc.mom.base.vo.BomVO;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 产品BOMController
 *
 * @author fwj
 * @since 2025-07-10
 */
@Tag(name = "产品BOM控制器", description = "产品BOM控制器")
@RestController
@RequestMapping("/base/bom")
public class BomController {
    @Autowired
    private BomService bomService;

    @Operation(summary = "查询产品BOM列表", description = "查询产品BOM列表")
    @PostMapping("/pageList")
    public Result<IPage<Bom>> pageList(@Parameter(description = "查询参数") @RequestBody BomParam bomParam) {
        Page<Bom> page = new Page<>(bomParam.getPage(), bomParam.getPageSize());
        Bom bom = new Bom();
        BeanUtils.copyProperties(bomParam, bom);
        QueryWrapper<Bom> wrapper = QueryWrapperUtil.build(bom);
        IPage<Bom> list = bomService.page(page, wrapper);
        return Result.success(list);
    }


    @Operation(summary = "获取产品BOM详细信息", description = "获取产品BOM详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "产品BOMID") @PathVariable("id") Long id) {
        return Result.success(bomService.getById(id));
    }
    @Operation(summary = "新增产品BOM", description = "新增产品BOM")
    @PostMapping
    public Result<?> save(@Parameter(description = "产品BOM") @RequestBody BomVO bomVO) {
        bomService.saveBom(bomVO);
        return Result.success();
    }

    @Operation(summary = "修改产品BOM", description = "修改产品BOM")
    @PutMapping
    public Result<?> update(@Parameter(description = "产品BOM") @RequestBody BomVO bomVO) {
        bomService.updateBom(bomVO);
        return Result.success();
    }

    @Operation(summary = "删除产品BOM", description = "删除产品BOM")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        bomService.removeBom(Arrays.asList(ids));
        return Result.success();
    }

    @Operation(summary = "更新状态", description = "更新状态")
    @PostMapping("/status")
    public Result<?> updateStatus(@RequestBody Bom bom) {
        bomService.updateStatus(bom);
        return Result.success();
    }

    @Operation(summary = "设置为默认版本", description = "更新状态")
    @PostMapping("/default")
    public Result<?> setDefault(@RequestBody Bom bom) {
        bomService.setDefault(bom);
        return Result.success();
    }

    @Operation(summary = "获取产品BOM物料", description = "获取产品BOM物料")
    @GetMapping("/product/{productId}")
    public Result<List<BomItem>> getBomByProduct(@PathVariable("productId") Long productId) {
        List<BomItem> bomItemList = bomService.getBomByProduct(productId);
        return Result.success(bomItemList);
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Bom> util = new ExcelUtil<>(Bom.class);
        util.importTemplateExcel(response, "产品BOM");
    }

    @Operation(summary = "导出产品BOM列表", description = "导出产品BOM列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, BomParam bomParam) {
        Bom bom = new Bom();
        BeanUtils.copyProperties(bomParam, bom);
        QueryWrapper<Bom> wrapper = QueryWrapperUtil.build(bom);
        List<Bom> list = bomService.list(wrapper);
        ExcelUtil<Bom> util = new ExcelUtil<>(Bom.class);
        util.exportExcel(response, list, "产品BOM数据");
    }

    @Operation(summary = "导入产品BOM列表", description = "导入产品BOM列表")
    @PostMapping("/import")
    public Result<?> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<Bom> util = new ExcelUtil<>(Bom.class);
        List<Bom> bomList = util.importExcel(file.getInputStream());
        String message = bomService.importData(bomList, updateSupport);
        return Result.success(message);
    }
}
